<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./swiper/css/swiper.css">
  <style>
    #swiper .swiper-slide {
      height: 300px;
      background-color: rgb(70, 196, 53);
    }

    #swiper .swiper-slide:nth-child(2n) {
      background-color: rgb(20, 92, 160);
    }
  </style>
</head>

<body>
  <div id="app">
    <button @click="isShow = !isShow">{{isShow?'隐藏':'显示'}}</button>
    <hr>
    <common-head v-if="isShow"></common-head>
  </div>
  <script src="./swiper/js/swiper.js"></script>
  <script src="./vue.js"></script>
  <script>
    const CommonHead = {
      template: `
      <div>
        <h3>这是头部组件</h3>
        <button @click="addLi">按钮</button>
        <div class="swiper-container" id="swiper">
          <div class="swiper-wrapper">
            <div class="swiper-slide" v-for="item in arr" :key="item">{{item}}</div>
          </div>
        </div>
      </div>
      `,
      data() {
        return {
          arr: [],
          swiper: null
        }
      },
      created() {
        this.fetchData();
      },
      mounted() {
        this.initSwiper();
      },
      watch: {
        arr(){
          // console.log('arr改变了');
          this.$nextTick(() => {
            this.swiper.update();
          })
        }
      },
      methods: {
        addLi(){
          this.arr.push(this.arr.length+1);
          
        /*   this.$nextTick(() => {
            console.log(document.querySelectorAll('.swiper-slide'),222);
          })
          console.log(document.querySelectorAll('.swiper-slide'),111); */
        },
        initSwiper() {
          
            this.swiper = new Swiper('#swiper', {
            autoplay: true,//可选选项，自动滑动
          })
        },
        fetchData() {
          // 发送ajax拿到数据赋值
          setTimeout(() => {
            this.arr = [1, 2, 3, 4];
          
          }, 2000)
        }
      }
    };
    Vue.component('CommonHead', CommonHead)
    const vm = new Vue({
      el: '#app',
      data: {
        isShow: true
      }
    })

  </script>
</body>

</html>